package vaani.shabd;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import vaani.internal.beagle.Interface;
import vaani.internal.beagle.Result;

/**
 *
 * @author sourcemorph
 */

public class Locator {
    /*high level class to perform desktop search, carries its own searching procedure when it comes to multiple search terms*/

    private static Vector<String> UnnecessaryNouns;

    static {
        UnnecessaryNouns = new Vector<String>();
        try {
            FileInputStream fstream = new FileInputStream("data/unnecessarynouns.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fstream));
            String temp;
            while((temp=reader.readLine()) != null) {
                UnnecessaryNouns.add(temp);
            }
            reader.close();
            fstream.close();
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }

    private static boolean isUnnecessary(String noun) {
        if(UnnecessaryNouns.contains(noun))
            return true;
        return false;
    }

    /*this method returns a vector containing all the hits*/
    public static Vector<Result> find(Vector<String> searchTerms) {
        String query = "";
        Vector<Result> temp;
        for(String s : searchTerms) {
            query += s;
        }
        /*first pass is made with all the nouns*/
        Interface.makeQuery(query);
        if(Interface.numHits() == 1) {
            return Interface.getHits();
        }
        else {
            /*second pass is made after removing unnecessary nouns such as "file" etc*/
            temp = Interface.getHits();
            query = "";
            for(String s : searchTerms) {
                if(!isUnnecessary(s))
                    query += s;
            }
            Interface.makeQuery(query);
            if(Interface.numHits() != 0) {
                return Interface.getHits();
            }
            else {
                if(temp.size() != 0) {
                    return temp;
                }
                else {
                    /*third pass with one less argument*/
                }
            }
        }
        return null;
    }
}
